package com.prs.crm.action.crm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.crm.CustomerContract;
import com.prs.crm.domain.crm.CustomerSupplier;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.crm.CustomerContractService;

public class CustomerContractAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private PaginationSupport pagination;
	
	private CustomerContract customerContract;
	private CustomerContractService customerContractService;
	private CustomerSupplier customerSupplier;
	private Integer[] contractIds;
	
	@Action("listCustomerContract")
	public String listCustomerContract(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerContract.class);
		Person per = AppContext.getAccountContext().currentUser();
//		criteria.createAlias("recorder", "rd");
		criteria.add(Restrictions.or(Restrictions.like("sharePerson","%"+per.getResume().getName()+"%"), Restrictions.like("siningPerson","%"+per.getResume().getName()+"%")));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerContractService.listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action("editCustomerContract")
	public String editCustomerContract(){
		this.customerContract=this.customerContractService.get(contractIds[0]);
		this.customerSupplier=this.customerContract.getCustomerSupplier();
		return SUCCESS;
	}

	@Action(value = "removeCustomerContract", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerContract"),
			@Result(name = "error", type = "chain", location = "listCustomerContract")})
	public String removeCustomerContract(){
		this.customerContractService.batchRemove(contractIds);
		return SUCCESS;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public CustomerContract getCustomerContract() {
		return customerContract;
	}

	public void setCustomerContract(CustomerContract customerContract) {
		this.customerContract = customerContract;
	}

	public CustomerContractService getCustomerContractService() {
		return customerContractService;
	}

	public void setCustomerContractService(
			CustomerContractService customerContractService) {
		this.customerContractService = customerContractService;
	}

	public Integer[] getContractIds() {
		return contractIds;
	}

	public void setContractIds(Integer[] contractIds) {
		this.contractIds = contractIds;
	}
	public CustomerSupplier getCustomerSupplier() {
		return customerSupplier;
	}
	public void setCustomerSupplier(CustomerSupplier customerSupplier) {
		this.customerSupplier = customerSupplier;
	}
	
}
